package com.thinkgem.jeesite.modules.website;

import com.thinkgem.jeesite.modules.area_info.service.AreaInfoService;
import com.thinkgem.jeesite.modules.guide.entity.Guiding;
import com.thinkgem.jeesite.modules.signallight.entity.TSignalLight;
import com.thinkgem.jeesite.modules.signallight.service.TSignalLightService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xlp
 * @date 2019/5/15
 */
@RestController
@RequestMapping("/signLight")
public class SignLightInfoController {

    @Autowired
    private AreaInfoService areaInfoService;

    @Autowired
    private DictService dictService;

    @Autowired
    private TSignalLightService signalLightService;

    /**
     * 获取单个车道信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/getSignLight", method = RequestMethod.POST)
    public TSignalLight getSignLight(HttpServletRequest request, String id) {
        TSignalLight signalLight = signalLightService.get(id);
        return signalLight;
    }

    /**
     * 获取信号灯信息集合
     * @param request
     * @return
     */
    @RequestMapping(value = "/getSignLightList", method = RequestMethod.POST)
    public ResponseData getSignLightList(HttpServletRequest request) {
        List<TSignalLight> signalLightList = signalLightService.findList(new TSignalLight());
        ResponseData responseData = new ResponseData();
        if (!CollectionUtils.isEmpty(signalLightList)){
            for (TSignalLight signalLight:signalLightList){
                /*AreaInfo areaInfo = areaInfoService.get(signalLight.getAreaid());
                if (areaInfo!=null){
                    signalLight.setAreaName(areaInfo.getAreaName());
                }*/

                List<Dict> dictList = findDictList("light_direction", signalLight.getLightdirection());
                if (!CollectionUtils.isEmpty(dictList)){
                    signalLight.setLightdirection(dictList.get(0).getLabel());
                }

                dictList = new ArrayList<Dict>();
                dictList = findDictList("light_status", signalLight.getStatus());
                if (!CollectionUtils.isEmpty(dictList)){
                    signalLight.setStatus(dictList.get(0).getLabel());
                }
            }
            responseData.setCode(200);
            responseData.setMsg("查询成功");
            responseData.setTotal(String.valueOf(signalLightList.size()));
            responseData.setData(signalLightList);
        }else {
            responseData.setCode(200);
            responseData.setMsg("查询失败");
            responseData.setTotal("0");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 新增或修改信号信息
     * @param request
     * @param signalLight
     */
    @RequestMapping(value = "/addOrUpdateLight", method = RequestMethod.POST)
    public String addOrUpdateLight(HttpServletRequest request, TSignalLight signalLight) {
        signalLightService.save(signalLight);
        return "";
    }

    /**
     * 获取信号灯方向字典
     * @param request
     */
    @RequestMapping(value = "/getLightDirection", method = RequestMethod.POST)
    public List<Dict> getLightDirection(HttpServletRequest request) {
        Dict dict = new Dict();
        dict.setType("light_direction");
        List<Dict> dictList = dictService.findList(dict);
        return dictList;
    }

    /**
     * 获取信号灯状态字典
     * @param request
     */
    @RequestMapping(value = "/getLightStatus", method = RequestMethod.POST)
    public List<Dict> getLightStatus(HttpServletRequest request) {
        Dict dict = new Dict();
        dict.setType("light_status");
        List<Dict> dictList = dictService.findList(dict);
        return dictList;
    }

    /**
     * 删除/批量删除信号灯信息
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(HttpServletRequest request, String id) {
        String res = "";
        String[] ids = id.split(",");
        TSignalLight signalLight = new TSignalLight();
        try {
            for (int i=0; i<ids.length; i++){
                signalLight.setId(ids[i]);
                signalLightService.delete(signalLight);
            }
            res = "200";
        }catch (Exception e){
            e.printStackTrace();
            res = "201";
        }

        return res;
    }

    /**
     * 根据type、value获取字典
     * @param type
     * @param value
     * @return
     */
    public List<Dict> findDictList(String type, String value){
        Dict dict = new Dict();
        dict.setValue(value);
        dict.setType(type);
        List<Dict> dictList = dictService.findList(dict);
        return dictList;
    }
}
